package com.leff.cards.ui.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.leff.cards.R;
import com.leff.cards.data.AppState;
import com.leff.cards.ui.GroupActivity;
import com.leff.cards.ui.util.GroupListAdapter;
import com.leff.cards.util.Secrets;
import com.leff.cards.web.QueryDispatcher;
import com.leff.cards.web.requests.CreateGroupRequest;
import com.leff.notifications.NotificationCenter;
import com.leff.notifications.NotificationListener;

public class GroupsFragment extends SherlockFragment implements NotificationListener, OnItemClickListener, Secrets {

	private GroupListAdapter mAdapter;
	private TextView mNoGroupsText;
	
	private Handler mHandler;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mAdapter = new GroupListAdapter(getActivity(), AppState.get().getGroups());
		this.setHasOptionsMenu(true);

		NotificationCenter.registerListener(this, AppState.GROUPS_REFRESHED);
		
		mHandler = new Handler();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if(container == null) {
			return null;
		}
		View V = inflater.inflate(R.layout.groups_fragment, null);
		
		mNoGroupsText = (TextView)V.findViewById(R.id.no_groups_text);
		ListView list = (ListView)V.findViewById(R.id.group_list);
		
		list.setOnItemClickListener(this);
		list.setAdapter(mAdapter);
		checkTextVisibility();
		
		return V;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		
		if(menu.findItem(R.id.menu_new_group) != null) {
			return;
		}
		inflater.inflate(R.menu.groups_menu, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		if(item.getItemId() == R.id.menu_new_group) {
			showCreateGroupDialog();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		NotificationCenter.unregisterListener(this, AppState.GROUPS_REFRESHED);
	}

	@Override
	public void onNotificationPosted(String action, Object data, Object sender) {
		
		// Lets us update from a notification on the background thread.
		mHandler.post(new Runnable() {
			public void run() {
				mAdapter.setData(AppState.get().getGroups());
				checkTextVisibility();
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> adapter, View V, int pos, long id) {
		
		AppState.get().setActiveGroup(id);
		Intent I = new Intent(getActivity(), GroupActivity.class);
		
		getActivity().startActivity(I);
	}
	
	private void checkTextVisibility() {
		if(mAdapter.getCount() != 0) {
			mNoGroupsText.setVisibility(View.GONE);
		}
		else {
			mNoGroupsText.setVisibility(View.VISIBLE);
		}
	}
	
	private void showCreateGroupDialog() {
		
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		
		builder.setTitle(R.string.create_group_title);
		
		final EditText nameField = new EditText(getActivity());
		final long userId = AppState.get().getUser().id;
		
		builder.setView(nameField);
		builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				String name = nameField.getText().toString().trim();
				if(name.length() == 0) {
					return;
				}
				
				new QueryDispatcher(new CreateGroupRequest(name, userId)).execute();
			}
		});
		builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		
		builder.create().show();
	}
}
